package com.geeklan.smart.ui.feed;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.Toast;

import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.geeklan.base.base.BaseActivity;
import com.geeklan.smart.R;
import com.geeklan.smart.data.model.FeedingRecord;
import com.geeklan.smart.data.repository.device.DeviceRepository;
import com.geeklan.smart.databinding.ActivityFeedingRecordsBinding;
import com.geeklan.smart.ui.device.DeviceListActivity;
import com.geeklan.smart.ui.feed.fragment.adapter.FeedingRecordsAdapter;
import com.geeklan.smart.ui.viewmodel.device.DeviceViewModel;
import com.geeklan.smart.ui.viewmodel.device.DeviceViewModelFactory;
import com.geeklan.smart.utils.DateUtils;
import com.geeklan.smart.widget.CalendarView;

import java.util.ArrayList;
import java.util.List;

public class FeedingRecordsActivity extends BaseActivity<ActivityFeedingRecordsBinding> implements FeedingRecordsAdapter.ItemOnClickListener {
    private FeedingRecordsAdapter mAdapter;
    private List<FeedingRecord> feedingRecords = new ArrayList<>();
    private DeviceViewModel deviceViewModel;

    private String deviceId;
    private String date;

    @Override
    protected ActivityFeedingRecordsBinding initBinding() {
        return ActivityFeedingRecordsBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        deviceId = getIntent().getStringExtra("deviceId");
        date = getIntent().getStringExtra("date");
        deviceViewModel = new ViewModelProvider(this, new DeviceViewModelFactory(new DeviceRepository())).get(DeviceViewModel.class);
        deviceViewModel.getFeedingRecordDeviceLiveData().observe(this, apiResponse -> {
            feedingRecords.clear();
            feedingRecords.addAll(apiResponse.getData());
            mAdapter.notifyDataSetChanged();
            if (feedingRecords.isEmpty()) {
                mBinding.rvFeedingRecords.setVisibility(View.GONE);
                mBinding.llNotData.setVisibility(View.VISIBLE);
            } else {
                mBinding.rvFeedingRecords.setVisibility(View.VISIBLE);
                mBinding.llNotData.setVisibility(View.GONE);
            }
            mBinding.smartRefreshLayout.finishRefresh();
        });

        deviceViewModel.getFeedingRecordDeviceErrorLiveData().observe(this, s -> {
            Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
            mBinding.smartRefreshLayout.finishRefresh();
        });
        mBinding.tvRecordsTitle.setText(DateUtils.formatTime1(date) +"记录");
        getGetMonthFeedingRecordLiveData();

    }

    @Override
    protected void initView() {
        mBinding.ivBack.setOnClickListener(v -> finish());
        mAdapter = new FeedingRecordsAdapter(this, feedingRecords);
        mAdapter.setItemOnClickListener(this);
        mBinding.rvFeedingRecords.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rvFeedingRecords.setAdapter(mAdapter);

        mBinding.smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            deviceViewModel.getFeedingRecordList(deviceId, date);
        });
        mBinding.smartRefreshLayout.autoRefresh();

        mBinding.calendarView.setOnDateClickListener(new CalendarView.OnDateClickListener() {
            @Override
            public void onDateClick(int year, int month, int day) {
                date = year + "-" + month + "-" + day;
                mBinding.tvRecordsTitle.setText(DateUtils.formatTime1(date) +"记录");
                mBinding.smartRefreshLayout.autoRefresh();
            }

            @Override
            public void onChangeMonth(int year, int month) {
                deviceViewModel.getMonthFeedingRecord(deviceId,year + "-" + (month < 10 ? "0" + month : month));
            }
        });
        mBinding.calendarView.getData();
    }

    private void getGetMonthFeedingRecordLiveData(){
        deviceViewModel.getGetMonthFeedingRecordLiveData().observe(this, apiResponse -> {
            mBinding.calendarView.generateCalendarDays(apiResponse.getData());
        });
    }

    @Override
    protected void onApplyWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            getWindow().setStatusBarColor(Color.WHITE);
            return insets;
        });
    }

    @Override
    public void networkStatus(boolean isConnected) {

    }

    @Override
    public void networkType(int type) {

    }

    public static void startActivity(Context context, String deviceId, String date) {
        Intent intent = new Intent(context, FeedingRecordsActivity.class);
        intent.putExtra("deviceId", deviceId);
        intent.putExtra("date", date);
        context.startActivity(intent);
    }

    @Override
    public void OnItemClick(View view, int position) {

    }
}